//
//  CZCommentController.swift
//  GZWeibo007
//
//  Created by ase on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD

class CZCommentController: UIViewController, UITextViewDelegate {
    
    func textViewDidChange(textView: UITextView) {
        navigationItem.rightBarButtonItem?.enabled = textView.hasText()
        
    }
    var status: CZStatus? {
        didSet{
            self.reloadInputViews()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        forwardTextView.delegate = self
        
        setupNav()
        
        view.addSubview(forwardTextView)
        forwardTextView.ff_AlignInner(type: ff_AlignType.TopCenter, referView: view, size: CGSize(width: CGFloat(UIScreen.mainScreen().bounds.size.width) - 10, height: 200), offset: CGPoint(x: 0, y: 5))
        
    }
    
    func setupNav() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "cencel")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "发送", style: UIBarButtonItemStyle.Plain, target: self, action: "send")
        
    }
    
    func cencel() {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    
    func send()
    {
        
        let statusString = (forwardTextView.attributedText.string as? NSString)!.substringWithRange(NSRange(location: 0, length: forwardTextView.attributedText.length))
        
        print("forwardTextView.attributedText.length\(forwardTextView.attributedText.length)")
        
        print("statusString\(statusString.characters.count) --\(statusString)")
        
        SVProgressHUD.showInfoWithStatus("正在发送中...", maskType: SVProgressHUDMaskType.Black)
        
        // 加载数据，将数据回传
        CZNetworkTool.sharedInstance.commtentStatus(status!.id, comment: statusString) { (result, error) -> () in
            
            if error != nil {
                SVProgressHUD.showErrorWithStatus("发送失败，请重新再试")
                print("error\(error)")
                return
            }
            
            SVProgressHUD.showInfoWithStatus("发送成功...", maskType: SVProgressHUDMaskType.Black)
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), { () -> Void in
                self.cencel()
            })
            
        }
        
        
    }
    
    lazy var forwardTextView:CZForwardTextView = {
        let forWardTextView = CZForwardTextView()
        
        forWardTextView.font = UIFont.systemFontOfSize(15)
        forWardTextView.textColor = UIColor.blackColor()
        forWardTextView.text = "评论"
        
        return forWardTextView
    }()
    
}
